package com.ruoyi.system.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.Warehouse;
import com.ruoyi.system.service.IWarehouseService;
import com.ruoyi.system.vo.ItemVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Item;
import com.ruoyi.system.service.IItemService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 物料Controller
 * 
 * @author ruoyi
 * @date 2024-12-12
 */
@RestController
@RequestMapping("/wms/item")
public class ItemController extends BaseController
{
    @Autowired
    private IItemService itemService;
    @Autowired
    private IWarehouseService iWarehouseService;

    /**
     * 查询物料列表
     */
    @PreAuthorize("@ss.hasPermi('wms:item:list')")
    @GetMapping("/selectItem")
    public TableDataInfo selectItem(Item item)
    {
        startPage();
        List<ItemVo> list = itemService.selectItem(item);
        return getDataTable(list);
    }
    /**
     * 查询物料列表
     */
    @PreAuthorize("@ss.hasPermi('wms:item:list')")
    @GetMapping("/list")
    public TableDataInfo list(Item item)
    {
        startPage();
        List<ItemVo> list = itemService.selectItemList(item);
        return getDataTable(list);
    }



    /**
     * 导出物料列表
     */
    @PreAuthorize("@ss.hasPermi('wms:item:export')")
    @Log(title = "物料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Item item)
    {
        List<ItemVo> list = itemService.selectItemList(item);
        ExcelUtil<ItemVo> util = new ExcelUtil<ItemVo>(ItemVo.class);
        util.exportExcel(response, list, "物料数据");
    }

    /**
 * 获取物料详细信息
 */
@PreAuthorize("@ss.hasPermi('wms:item:query')")
@GetMapping(value = "/{id}")
public AjaxResult getInfo(@PathVariable("id") Long id)
{
    return success(itemService.selectItemById(id));
}

    /**
     * 新增物料
     */
    @PreAuthorize("@ss.hasPermi('wms:item:add')")
    @Log(title = "物料", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Item item)
    {
        return toAjax(itemService.insertItem(item));
    }

    /**
     * 修改物料
     */
    @PreAuthorize("@ss.hasPermi('wms:item:edit')")
    @Log(title = "物料", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Item item)
    {
        return toAjax(itemService.updateItem(item));
    }

    /**
     * 删除物料
     */
    @PreAuthorize("@ss.hasPermi('wms:item:remove')")
    @Log(title = "物料", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(itemService.deleteItemByIds(ids));
    }

    /**
     * 获取分类
     */
    @PreAuthorize("@ss.hasPermi('wms:item:query')")
    @GetMapping(value = "/getItemType")
    public AjaxResult getItemType()
    {
        return success(itemService.getItemType());
    }

    /**
     * 查询物料列表
     */
    @PreAuthorize("@ss.hasPermi('wms:item:list')")
    @GetMapping("/select")
    public TableDataInfo select()
    {
        List<Map<String, Object>> maps = itemService.selectWarehouserItem();
//        for (Map<String, Object> map : maps) {
//            for (Map.Entry<String, Object> vo : map.entrySet()) {
//                String key = vo.getKey();
//                Object value = vo.getValue();
//                //调用物料单个查询
//                //调用仓库单个查询查出仓库
//            }
//        }


        return getDataTable(maps);
    }

    /**
     * 修改删除标识
     */
    @PreAuthorize("@ss.hasPermi('wms:item:edit')")
    @Log(title = "物料", businessType = BusinessType.UPDATE)
    @PutMapping("/editDelFlag")
    public AjaxResult editDelFlag(String itemNo)
    {
        return toAjax(itemService.editDelFlag(itemNo));
    }
}
